import 'package:dk_flutter_base/dk_ui.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_dk_form/src/bean/navigation_bar_bean.dart';

/// 导航栏
/// [showBack]:返回是否显示
/// [backClickListener]:返回点击回调
/// [title]:标题
/// [rightWidget]:右侧Widget
/// [rightWidgetClickListener]:右侧Widget点击回调
/// [hasLine]:是否有下划线
/// [itemList]:menu列表
class NavigationBarWidget extends StatelessWidget {
  final bool showBack;
  final VoidCallback backClickListener;
  final String title;
  final Widget rightWidget;
  final VoidCallback rightWidgetClickListener;
  final bool hasLine;
  final List<NavigationBarBean> itemList;

  NavigationBarWidget(
      {this.showBack,
      this.backClickListener,
      this.title,
      this.rightWidget,
      this.rightWidgetClickListener,
      this.hasLine = true,
      this.itemList});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Container(
            width: double.infinity,
            height: 44,
            color: Colors.white,
            child: Stack(
              children: <Widget>[
                Container(
                  width: double.infinity,
                  alignment: Alignment.center,
                  child: Text(
                    title,
                    style: TextStyle(color: Color(0xFF333333), fontSize: 17),
                  ),
                ),
                Row(
                  children: <Widget>[
                    GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: backClickListener,
                      child: Container(
                        padding: EdgeInsets.all(14),
                        child: Image(
                            height: 16,
                            image: AssetImage(
                                "packages/flutter_dk_form/images/dk_form/navigation_bar_back.png")),
                      ),
                    ),
                    Spacer(),
                    GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: rightWidgetClickListener,
                      child: Container(
                        padding: EdgeInsets.only(right: 14),
                        alignment: Alignment.centerLeft,
                        height: double.infinity,
                        child: rightWidget,
                      ),
                    ),
                    Visibility(
                        visible: (itemList != null && itemList.length > 0),
                        child: PopupMenuButton(
                            child: Container(
                              padding: EdgeInsets.all(14),
                              child: Image(
                                height: 16,
                                image: AssetImage(
                                    "packages/flutter_dk_form/images/dk_form/navigation_bar_menu.png"),
                                width: 16,
                              ),
                            ),
                            onSelected: (item) {
                              print("zmzm" + item);
                            },
                            offset: Offset(300, 100),
//                            offset : Offset.zero,
                            itemBuilder: (builder) => getPopupMenuItems()))
                  ],
                ),
              ],
            )),
        Visibility(
            visible: hasLine,
            child: Divider(height: 1, color: Color(0xFFEBEBEB))),
      ],
    );
  }

  getPopupMenuItems() {
    List<PopupMenuEntry> list = List<PopupMenuEntry>();
    if (itemList != null && itemList.length > 0) {
      for (var value in itemList) {
        list.add(
          PopupMenuItem(
            value: value.id,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                (value.iconPath != null && value.iconPath.isNotEmpty)
                    ? Padding(
                        padding: EdgeInsets.only(top: 3, right: 9),
                        child: Image(
                          height: 16,
                          image: AssetImage(value.iconPath),
                          width: 16,
                        ),
                      )
                    : SizedBox(),
                Text(
                  value.title,
                  style: TextStyle(fontSize: 16, color: Color(0xff333333)),
                ),
              ],
            ),
          ),
        );
      }
    }
    return list;
  }
}
